<?php
/**
 * Created by PhpStorm.
 * User: rocky
 * Date: 2019-07-14
 * Time: 00:51
 */

namespace app\common\model;


use app\common\facade\Token;
use library\File;
use think\db\Query;
use think\facade\Request;
use think\Model;

/**
 * Class BaseModel
 * @package app\common\model
 * @method $this uid() uid查询条件
 * @method $this pages() 分页条件
 * @method $this isDel() 只查询软删除数据
 * @method $this withDel() 查询包含软删除数据
 */
class BaseModel extends Model
{

    public function base(Query $query)
    {
        $id = $query->getPk();
        $tableFields = $query->getTableFields();
        //默认排序
        if (in_array('sort', $tableFields)) {
            $query->order('sort asc')->order("{$id} desc");
        } else {
            $query->order("{$id} desc");
        }
        //默认不包含软删除数据
        if (in_array('is_deleted', $tableFields)) {
            $query->where('is_deleted',0);
        }
    }

    private function imgUrl($val)
    {
        if(!strstr($val, 'http://') && !strstr($val, 'https://') && !empty($val)) {
            $val = File::instance('local')->url(str_replace('upload/', '', $val));
            if ($val == false) {
                return null;
            }
        }
        return $val;
    }
    protected function getImgUrl($val)
    {
        if (is_array($val)) {
            $imgs = array_filter($val);
            $val = [];
            foreach ($imgs as $img) {
                $val[] = $this->imgUrl($img);
            }
            return $val;
        } else {
            return $this->imgUrl($val);
        }
    }
    //只查询软删除数据
    public function scopeIsDel($query){
        $query->removeWhereField('is_deleted')->where('is_deleted',1);
    }
    //查询包含软删除数据
    public function scopeWithDel($query){
        $query->removeWhereField('is_deleted');
    }
    //分页条件
    public function scopePages($query,$page=1,$size=10)
    {
        $page = Request::param('page', $page);
        $size = Request::param('size', $size);
        $query->page($page, $size);
    }

    //查询当前用户条件
    public function scopeUid($query)
    {
        $uid = Token::id();
        $query->where('uid', $uid);
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'uid');
    }
    /**
     * 生成select树形选项
     * @param array $nodes
     * @param int $parentId 父级id
     * @param string $prefix 输出格式前缀
     * @param string $space 间隔字符
     * @param string $titleField 标题字段
     * @param string $pk 主键
     * @param string $parentField 父级字段
     * @param string $sortField 排序字段
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function buildTreeSelectOptions($nodes = [], $parentId = 0,$prefix = '', $space = '&nbsp;',$titleField='title', $pk='id',$parentField='pid',$sortField='sort')
    {
        $prefix = $prefix ?: '┝'.$space;

        $options = [];

        if (empty($nodes)) {
            if (in_array('is_deleted', self::getTableFields())) {
                $nodes = self::removeOption('order')->where('is_deleted',0)->order($sortField)->select();
            }else{
                $nodes = self::removeOption('order')->order($sortField)->select();
            }

        }
        foreach ($nodes as $index => $node) {
            if ($node[$parentField] == $parentId) {
                $node[$titleField] = $prefix.$space.$node[$titleField];

                $childrenPrefix = str_replace('┝', str_repeat($space, 6), $prefix).'┝'.str_replace(['┝', $space], '', $prefix);

                $children = self::buildTreeSelectOptions($nodes, $node[$pk], $childrenPrefix,$space ,$titleField, $pk,$parentField,$sortField);

                $options[$node[$pk]] = $node[$titleField];

                if ($children) {
                    $options += $children;
                }
            }
        }

        return $options;
    }
}